package com.leadership.api.service.impl;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.CollectionUtils;
import com.leadership.api.dao.EvaluationDao;
import com.leadership.api.dao.UserProjectDao;
import com.leadership.api.handler.SurveyHandler;
import com.leadership.api.service.SurveyService;
import com.leadership.entity.evaluation.Evaluation;
import com.leadership.entity.evaluation.EvaluationType;
import com.leadership.entity.project.UserProject;
import com.leadership.entity.survey.SurveyProject;
import com.leadership.entity.survey.SurveyResult;

/**
 * 
 * @author mohanlan
 *
 */
@Service("surveyService")
public class SurveyServiceImpl implements SurveyService {
    
    //private static final Logger logger = LoggerFactory.getLogger(SurveyService.class);

    @Autowired
    private SurveyHandler surveyHandler;

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    @Dao
    private UserProjectDao userProjetDao;

    @Dao
    private EvaluationDao evaluationDao;

    /*
     * (non-Javadoc)
     * 
     * @see com.leadership.api.service.SurveyService#syncItems()
     */
    public void syncItems() {

        List<SurveyProject> surveyProjects = surveyHandler.syncSurveyItems();
        for (SurveyProject surveyProject : surveyProjects) {
            SurveyProject exit =
                    genericDaoSupport.searchForObject("FROM SurveyProject WHERE id = :id", "id",
                            surveyProject.getId(), SurveyProject.class);
            if (exit != null) {
                genericDaoSupport.update(exit.update(surveyProject));
            } else {
                genericDaoSupport.save(surveyProject);
            }

        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see com.leadership.api.service.SurveyService#updateSurveyStatus(java.util.List,
     * java.lang.Long)
     */
    public void updateSurveyStatus(List<SurveyResult> results, Long projectId, String type) {

        List<UserProject> userProjects = userProjetDao.listUserProject(projectId);
        //logger.info("get projects [{}]", CollectionUtils.extractPropertyList(userProjects, "id"));
        
        Map<String, SurveyResult> suMap = CollectionUtils.constructMapValue(results, "userMail");

        for (UserProject userProject : userProjects) {

            String email = userProject.getUser().getEmail();
            //logger.info("get email [{}]", email);
            EvaluationType evaluationType = type.contains("360")
                    ? EvaluationType.THREE_SIX_ZERO_EVALUATION : EvaluationType.SELF_AWARENESS;
            
            //logger.info("get evaluationType [{}]", evaluationType);
            
            Evaluation evaluation = evaluationDao.load(projectId, email, evaluationType);

            SurveyResult surveyResult = suMap.get(email);
            if(surveyResult == null) {
                surveyResult = suMap.get(email.toUpperCase());
            }
            //logger.info("get surveyResult [{}]", JsonUtils.toJson(surveyResult));
            
            if (surveyResult != null && surveyResult.getStatus().equals("finish")) {
                if (evaluation == null) {
                    evaluation = new Evaluation(userProject.getUser(), projectId, surveyResult,
                            evaluationType);
                    genericDaoSupport.save(evaluation);
                } else {
                    //logger.info("get evaluation [{}]", evaluation.getId());
                    genericDaoSupport.update(evaluation.update(surveyResult));
                }
                
                if(evaluationType == EvaluationType.THREE_SIX_ZERO_EVALUATION) {
                    userProject.setEvaluationCompleted(true);
                } else {
                    userProject.setSelfAwarenessCompleted(true);
                } 
                genericDaoSupport.update(userProject);
                
            } else {
                
                if(evaluation != null) {
                    genericDaoSupport.delete(evaluation);
                }
                
                if(evaluationType == EvaluationType.THREE_SIX_ZERO_EVALUATION) {
                    userProject.setEvaluationCompleted(false);
                } else {
                    userProject.setSelfAwarenessCompleted(false);
                } 
                genericDaoSupport.update(userProject);
            }
        }

    }

}
